package client.behavior;

import javax.vecmath.Vector3d;

import client.object.CObject3DClient;

import server.object.*;
import wiiremotej.*;
import wiiremotej.event.*;

class WiimoteListener implements WiiRemoteListener {

	private Interactor interactor;
	private WiiRemote wiimote;
	private CObject3DClient object;
	
	public WiimoteListener(WiiRemote wiimote, CObject3DClient object){
		this.wiimote = wiimote;
		interactor = new InteractorImpl();
		this.object = object;
	}
	
	@Override
	public void IRInputReceived(WRIREvent ire) {
		/*for (int i = 0 ; i < ire.getIRLights ().length ; i++) {
			if (ire.getIRLights () [i] != null) {
				System.out.println (ire.getIRLights () [i].getX () + " " + ire.getIRLights () [i].getY ()) ;
			}
		}*/
	}

	@Override
	public void accelerationInputReceived(WRAccelerationEvent ae) {
		
	}

	@Override
	public void buttonInputReceived (WRButtonEvent be) {
		if (be.wasPressed (WRButtonEvent.LEFT)) System.out.println ("Left pressed") ;
		if (be.wasPressed (WRButtonEvent.RIGHT)) System.out.println ("Right pressed") ;
		if (be.wasPressed (WRButtonEvent.DOWN)){
			Vector3d vectUp = new Vector3d(0.0,-0.5,0.0);
			Interactor interactorUp = new InteractorImpl();
			interactorUp.setInteractiveObject(object);
			interactorUp.translateInGlobal(vectUp);
		}
		if (be.wasPressed (WRButtonEvent.UP)){
			Vector3d vectUp = new Vector3d(0.0,0.5,0.0);
			Interactor interactorUp = new InteractorImpl();
			interactorUp.setInteractiveObject(object);
			interactorUp.translateInGlobal(vectUp);
		}
		if (be.wasReleased (WRButtonEvent.A)) {
			System.out.println ("A released");
		}
	}

	@Override
	public void combinedInputReceived(WRCombinedEvent arg0) {
		//System.out.println ("combinedInputReceived") ;
	}

	@Override
	public void disconnected() {
		System.out.println ("disconnected") ;
	}

	@Override
	public void extensionConnected(WiiRemoteExtension arg0) {
		System.out.println ("extensionConnected") ;
		try {
			wiimote.setExtensionEnabled (true) ;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void extensionDisconnected(WiiRemoteExtension arg0) {
		System.out.println ("extensionDisconnected") ;
	}

	@Override
	public void extensionInputReceived(WRExtensionEvent ee) {
		/*if (ee instanceof WRNunchukExtensionEvent) {
			WRNunchukExtensionEvent nee = (WRNunchukExtensionEvent)ee ;
			WRAccelerationEvent ae = nee.getAcceleration () ;
			System.out.println ("nunchuk x acceleration : " + ae.getXAcceleration ()) ;
			System.out.println ("nunchuk y acceleration : " + ae.getYAcceleration ()) ;
			System.out.println ("nunchuk z acceleration : " + ae.getZAcceleration ()) ;
			if (nee.wasReleased (WRNunchukExtensionEvent.C)) System.out.println ("C released") ;
			if (nee.wasReleased (WRNunchukExtensionEvent.Z)) System.out.println ("Z released") ;
			if (nee.wasPressed (WRNunchukExtensionEvent.C)) System.out.println ("C pressed") ;
			if (nee.wasPressed (WRNunchukExtensionEvent.Z)) System.out.println ("Z pressed") ;
			double x = nee.getAnalogStickData ().getX () ;
			double y = nee.getAnalogStickData ().getY () ;
			if ((Math.abs (x) > 0.02) && (Math.abs (y) > 0.02)) {
				System.out.println ("stick x = " + x + " ; y = " + y + " ; angle = " + nee.getAnalogStickData ().getAngle ()) ;
			}
		}*/
	}

	@Override
	public void extensionPartiallyInserted() {
		System.out.println ("extensionPartiallyInserted") ;
	}

	@Override
	public void extensionUnknown() {
		System.out.println ("extensionUnknown") ;
	}

	@Override
	public void statusReported(WRStatusEvent arg0) {
		System.out.println ("statusReported") ;
	}

}